﻿using UploadClient.Framework;
using UploadClient.Interfaces;

namespace UploadClient.ViewModel
{
    class SettingsWindowViewModel : ViewModelBase
    {
        //public bool isTriggertByButton = false;

        ///// <summary>
        ///// Binding to the View: IsFavourite
        ///// </summary>
        //Favourite favourite = new Favourite(false);

        private string adressString = "myDomain.com/uploadFolder";

        private string nameString = string.Empty;
        private bool passiveMode;

        private string passwordString = string.Empty;

        private int portNumber;

        public SettingsWindowViewModel(ICommunication communication)
        {
            Initial();

            FavouriteClick = new DelegateCommand(FavouriteClick_Execute, FavouriteClick_CanExecute);
            DeleteClick = new DelegateCommand(DeleteClick_Execute, DeleteClick_CanExecute);
            SaveClick = new DelegateCommand(SaveClick_Execute, SaveClick_CanExecute);

            Communication = communication;
        }

        public string AdressString
        {
            get
            {
                return adressString;
            }
            set
            {
                adressString = value;
                OnPropertyChanged("AdressString");
            }
        }

        public string NameString
        {
            get
            {
                return nameString;
            }
            set
            {
                nameString = value;
                OnPropertyChanged("NameString");
            }
        }

        public string PasswordString
        {
            get
            {
                return passwordString;
            }
            set
            {
                passwordString = value;
                OnPropertyChanged("PasswordString");
            }
        }

        public int PortNumber
        {
            get
            {
                return portNumber;
            }
            set
            {
                portNumber = value;
                OnPropertyChanged("PortNumber");
            }
        }

        public bool PassiveMode
        {
            get
            {
                return passiveMode;
            }
            set
            {
                passiveMode = value;
                OnPropertyChanged("PassiveMode");
            }
        }

        public DelegateCommand FavouriteClick { get; private set; }
        public DelegateCommand DeleteClick { get; private set; }
        public DelegateCommand SaveClick { get; private set; }

        private void Initial()
        {
            //this.DataContext = favourite;

            ////set default values in gui
            //if (settings.Count() > 0)
            //{
            //    if (settings.FavouriteConnection != null)
            //    {
            //        SetControlValues(settings.FavouriteConnection);
            //    }
            //    else
            //        SetControlValues(settings.First());

            //    OkButton.IsEnabled = true;
            //}
            //else
            //{
            //    SetControlValues(new ServerSettings());
            //    EnableButtons();
            //}

            //HostadressField.Focus();
        }

        private void FavouriteClick_Execute(object param)
        {
            //var result = settings.Find(c => c.Equals(GetControlValues()));
            //if (result != null)
            //{
            //    if (settings.FavouriteConnection != null && settings.FavouriteConnection.Equals(result))
            //    {
            //        settings.FavouriteConnection = null;
            //        favourite.IsFavourite = false;
            //    }
            //    else
            //    {
            //        settings.FavouriteConnection = result;
            //        favourite.IsFavourite = true;
            //    }
            //}
            //else favourite.IsFavourite = !favourite.IsFavourite;
        }

        private void DeleteClick_Execute(object param)
        {
            var result = Communication.ConSettings.Find(c => c.Equals(GetControlValues()));
            if (result != null)
                Communication.ConSettings.Remove(result);

            SetControlValues(new ServerSettings());
        }

        private void SaveClick_Execute(object param)
        {
            ServerSettings connection = Communication.ConSettings.Find(c => c.Equals(GetControlValues()));

            if (connection == null)
                connection = GetControlValues();

            Communication.ConSettings.Add(connection);
            Communication.ConSettings.SelectedConnection = connection;

            //if (favourite.IsFavourite)
            //    settings.FavouriteConnection = connection;

            //isTriggertByButton = true;

            OnRequestClose();
        }

        private bool FavouriteClick_CanExecute(object param)
        {
            return true;
        }

        private bool DeleteClick_CanExecute(object param)
        {
            return true;
        }

        private bool SaveClick_CanExecute(object param)
        {
            // disable/false if config is incomplete!
            return true;
        }

        private void SetControlValues(ServerSettings csettings)
        {
            //HostadressField.Text = csettings.Host;
            //UsernameField.Text = csettings.Username;
            //PasswordField.Password = csettings.Password;
            //PassivCheckBox.IsChecked = csettings.Passive;
            //PortField.Text = csettings.Port.ToString();

            //if (settings.FavouriteConnection != null && settings.FavouriteConnection.Equals(csettings))
            //    favourite.IsFavourite = true;
            //else favourite.IsFavourite = false;
        }

        private ServerSettings GetControlValues()
        {
            return new ServerSettings
            {
                Host = AdressString,
                Username = NameString,
                Password = PasswordString,
                Passive = PassiveMode,
                Port = PortNumber,
            };
        }

        //private void Hostadress_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    EnableButtons();
        //}

        ///// <summary>
        ///// Enable buttons if HostadressField has text.
        ///// Disable if not!
        ///// </summary>
        //private void EnableButtons()
        //{
        //    if (!HostadressField.Text.Equals(string.Empty))
        //    {
        //        StarButton.IsEnabled = true;
        //        DeleteButton.IsEnabled = true;
        //        OkButton.IsEnabled = true;
        //    }
        //    else
        //    {
        //        StarButton.IsEnabled = false;
        //        DeleteButton.IsEnabled = false;
        //        OkButton.IsEnabled = false;
        //    }
        //}
    }
}
